package com.veal98.stack_queue;

import java.util.Stack;

public class RemoveAllAdjacentDuplicatesInString_1047 {

    public static void main(String[] args) {

    }

    public String removeDuplicates(String s) {
        if (s == null || s.length() == 0 || s.length() == 1) {
            return s;
        }

        Stack<Character> stack = new Stack<>();

        for (Character ch : s.toCharArray()) {
            if (!stack.isEmpty() && stack.peek() == ch) {
                stack.pop();
                continue;
            }

            stack.push(ch);
        }

        // 栈中的字符就是消除了重复项的字符(注意这里的重复意思是：两个相邻且相同)
        StringBuilder res = new StringBuilder();
        while (!stack.isEmpty()) {
            res.append(stack.pop());
        }

        return res.reverse().toString();
    }

}
